import json
import re

from django import http
from django.contrib.auth import login, authenticate
from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View
from django_redis import get_redis_connection

from users.models import User

class LoginView(View):
    """实现登录接口"""
    def post(self,request):
        # 1.接收参数
        dict = json.loads(request.body.decode())
        username = dict.get('username')
        password = dict.get('password')
        remembered = dict.get('remembered')

        # 2.校验(整体 + 单个)
        if not all([username,password]):
            return JsonResponse({'code':400,'errmsg':'缺少必传参数'})
        # 3.验证是否能够登录
        user = authenticate(username=username,password=password)

        if user is None:
            return JsonResponse({'code':400,'errmsg':'用户名或密码错误'})

        # 4.状态保持
        login(request,user)

        # 5.判断是否记住用户
        if remembered != True:
            # 7.如果没有记住: 关闭立刻失效
            request.session.set_expiry(0)
        else:
            # 6.如果记住:  设置为两周有效
            request.session.set_expiry(None)

            # 8.返回json
            return JsonResponse({'code':0,'errmsg':'ok'})

class RegisterView(View):

    def post(self,request):
        '''接收参数, 保存到数据库'''
        # 1.接收参数
        dict = json.loads(request.body.decode())
        username = dict.get('username')
        password = dict.get('password')
        password2 = dict.get('password2')
        mobile = dict.get('mobile')
        allow = dict.get('allow')
        sms_code_client = dict.get('sms_code')

        # 2.校验(整体)
        if not all([username,password,password2,mobile,allow,sms_code_client]):
            return http.JsonResponse({'code':400,
                                      'errmsg':'缺少必传参数'})

        # 3.username检验
        if not re.match(r'^[a-zA-Z0-9_-]{5,20}$',username):
            return http.JsonResponse({'code': 400,
                                      'errmsg': 'username格式有误'})

        # 4.password检验
        if not re.match(r'^[a-zA-Z0-9]{8,20}$',password):
            return http.JsonResponse({'code': 400,
                                      'errmsg': 'password格式有误'})

        # 5.password2 和 password
        if password != password2:
            return http.JsonResponse({'code': 400,
                                      'errmsg': '两次输入不对'})
        # 6.mobile检验
        if not re.match(r'^1[3-9]\d{9}$',mobile):
            return http.JsonResponse({'code': 400,
                                      'errmsg': 'mobile格式有误'})
        # 7.allow检验
        if allow != True:
            return http.JsonResponse({'code': 400,
                                      'errmsg': 'allow格式有误'})

        # 8.sms_code检验 (链接redis数据库)
        redis_conn = get_redis_connection('verify_code')

        # 9.从redis中取值
        sms_code_server = redis_conn.get('sms_code_%s' %mobile)

        # 10.判断该值是否存在
        if not sms_code_server:
            return http.JsonResponse({'code': 400,
                                      'errmsg': '短信验证码过期'})
        # 11.把redis中取得值和前端发的值对比
        if sms_code_client != sms_code_server.decode():
            return http.JsonResponse({'code': 400,
                                      'errmsg': '验证码有误'})

        # 12.保存到数据库 (username password mobile)
        try:
            user = User.objects.create_user(username=username,
                                             password=password,
                                             mobile=mobile)
        except Exception as e:
            return http.JsonResponse({'code': 400,
                                      'errmsg': '保存到数据库出错'})
        # 添加如下代码
        # 实现状态保持
        login(request,user)

        # 13.拼接json返回
        return http.JsonResponse({'code': 0,
                                 'errmsg': 'ok'})









class UsernameCountView(View):
    """判断用户是否重复注册"""

    def get(self,request,username):
        """
       :param request: 请求对象
       :param username: 用户名
       :return: JSON
       """
        #获取数据库中该用户名对应的个数
        try:
            count = User.objects.filter(username=username).count()
        except Exception as e:
            return JsonResponse({
                'code':400,
                'errmsg':'访问数据库错误'

            })
        #拼接参数，返回数据
        return JsonResponse({
            'code':0,
            'errmsg':'ok',
            'count':count
        })

class MobileCountView(View):
    """手机号重复注册"""
    def get(self,request,mobile):
        '''
       判断电话是否重复, 返回对应的个数
       :param request:
       :param mobile:
       :return:
       '''
        try:
            # 1.从数据库中查询 mobile 对应的个数
            count = User.objects.filter(mobile=mobile).count()

        except Exception as e:
            return JsonResponse({
                'code':400,
                'errmsg':'访问数据库错误'

            })
        # 2.拼接参数, 返回结果
        return JsonResponse({
            'code':0,
            'errmsg':'ok',
            'count':count
        })